package com.xyy.saas.payment.service.auth.impl;

import com.alibaba.fastjson.JSONObject;
import com.xyy.saas.payment.pojo.bo.MenuBo;
import com.xyy.saas.payment.pojo.bo.QueryAccountBo;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import com.xyy.saas.payment.dao.model.MenuPermissionPo;
import com.xyy.saas.payment.dao.model.PayUserPo;
import com.xyy.saas.payment.service.MenuInfoService;
import com.xyy.saas.payment.service.MenuPermissionService;
import com.xyy.saas.payment.service.PayUserService;
import com.xyy.saas.payment.service.auth.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author zhangwenxiong
 * @Date 2020/11/4 18:04
 * @Version 1.0
 */
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private MenuPermissionService menuPermissionService;

    @Autowired
    private MenuInfoService menuInfoService;

    @Autowired
    private PayUserService payUserService;

    @Transactional
    @Override
    public ResultVO update(String json) {
        ResultVO resultVO = new ResultVO();
        try {
            if (StringUtils.isEmpty(json)) {
                resultVO.setCode("1");
                resultVO.setMsg("参数为空");
            } else {
                QueryAccountBo permissionBo = JSONObject.parseObject(json, QueryAccountBo.class);
                //更新菜单权限
                MenuPermissionPo permissionPo = new MenuPermissionPo();
                permissionPo.setMenuId(permissionBo.getMenuLine());
                permissionPo.setUserAccount(permissionBo.getAccount());
                MenuPermissionPo menuByAccount = menuPermissionService.getMenuByAccount(permissionPo.getUserAccount());
                //如果是空的赋值空字符串
                if (StringUtils.isEmpty(permissionBo.getMenuLine())) {
                    permissionPo.setMenuId("");
                    permissionPo.setHarf("");
                }else {
                    permissionPo.setMenuId(permissionBo.getMenuLine());
                    permissionPo.setHarf(permissionBo.getHarf());
                }
                if (null == menuByAccount) {
                    menuPermissionService.addMenu(permissionPo);
                } else {
                    menuPermissionService.updateMenu(permissionPo);
                }


                //更新数据权限
                PayUserPo userPo = new PayUserPo();
                userPo.setUserAccount(permissionBo.getAccount());
                userPo.setDataPermission(permissionBo.getDataPermission());
                if (null == userPo.getDataPermission()){
                    userPo.setDataPermission("");
                }
                payUserService.updateUser(userPo);
                resultVO.setCode("0");
                resultVO.setMsg("成功");
            }
        } catch (Exception e) {
            log.error("修改权限异常:{}", e);
            resultVO.setCode("1");
            resultVO.setMsg("修改权限异常,请联系管理员");
        }
        return resultVO;
    }

    /**
     * 将三级菜单转换成一行存储
     *
     * @param menuBos
     * @return
     */
    private String converMenuToLine(List<MenuBo> menuBos) {
        StringBuilder sb = new StringBuilder();
        //一级菜单
        for (MenuBo parent : menuBos) {
            sb.append(parent.getId());
            sb.append(",");
            if (CollectionUtils.isNotEmpty(parent.getChildren())) {
                //二级菜单
                for (MenuBo second : parent.getChildren()) {
                    sb.append(second.getId());
                    sb.append(",");
                    if (CollectionUtils.isNotEmpty(second.getChildren())) {
                        //三级菜单
                        for (MenuBo third : second.getChildren()) {
                            sb.append(third.getId());
                            sb.append(",");
                        }
                    }
                }
            }
        }
        String ids = sb.toString();
        ids = ids.substring(0, ids.length() - 1);
        return ids;
    }

    private List<MenuBo> groupMenu(List<MenuBo> menuBos) {
        List<MenuBo> finalMenu = new ArrayList<>();
        Map<String, List<MenuBo>> groupBos = menuBos.stream().filter(item->StringUtils.isNotEmpty(item.getParentId())).collect(Collectors.groupingBy(MenuBo::getParentId));
        for (MenuBo item : menuBos) {
            if (CollectionUtils.isNotEmpty(groupBos.get(item.getId()))) {
                item.setChildren(groupBos.get(item.getId()));
                finalMenu.add(item);
            }
        }
        return finalMenu;
    }
}
